<template>
    <div class="articles-add">
      <el-dialog
        title="提示"
        :visible="isShowAdd"
        width="50%"
        :before-close="handleClose"
        @open='open'>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="文章标题:" prop="title">
             <el-input v-model="form.title" placeholder="请输入文章标题"></el-input>
          </el-form-item>

          <el-form-item class="edit_container" label="文章内容:" prop="articleBody">

              <quill-editor
                  v-model="form.articleBody"
                  ref="myQuillEditor"
                  :options="editorOption"
                  @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
                  @change="onEditorChange($event)">
              </quill-editor>

          </el-form-item>
          <el-form-item label="视频地址:">
             <el-input v-model="form.videoURL" placeholder="请输入视频地址"></el-input>
          </el-form-item>

        </el-form>
          <template #footer>

            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="submitForm">确认</el-button>
          </template>

      </el-dialog>
    </div>

</template>
<script>
import { add, update } from '@/api/hmmm/articles'
import { quillEditor } from 'vue-quill-editor' // 调用编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  name: 'articlesAdd',
  components: {
    quillEditor
  },
  props: {
    isShowAdd: {
      type: Boolean,
      required: true
    },
    data: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      content: '<p></p><p><br></p><ol><li><strong><em>Or drag/paste an image here.</em></strong></li><li><strong><em>rerew</em></strong></li><li><strong><em>rtrete</em></strong></li><li><strong><em>tytrytr</em></strong></li><li><strong><em>uytu</em></strong></li></ol>',
      editorOption: {
        placeholder: '请在这里输入',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            [{ list: 'ordered' }, { list: 'bullet' }], // 列表
            ['blockquote'],
            ['code-block', 'image', 'link'] // 上传图片、上传视频
          ]
        }
      },
      form: {
        id: '',
        title: '',
        articleBody: null,
        videoURL: null
      },
      rules: {
        title: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    onEditorReady (editor) { // 准备编辑器

    },
    onEditorBlur () {}, // 失去焦点事件
    onEditorFocus () {}, // 获得焦点事件
    onEditorChange () {}, // 内容改变事件
    handleClose () {
      this.$emit('update:isShowAdd', false)
      this.$refs.form.resetFields()
      this.form = {
        id: '',
        title: '',
        articleBody: null,
        videoURL: null
      }
      this.$parent.data = {}
    },
    // 文章
    submitForm () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.form.id) {
            await update(this.form)
          } else {
            await add(this.form)
          }
          this.handleClose()
          this.$parent.getArticles()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    open () {
      const { id, title, articleBody, videoURL } = this.data
      this.form = { id, title, articleBody, videoURL }
    }
  },
  computed: {
    editor () {
      return this.$refs.myQuillEditor.quill
    }

  }

}
</script>
<style scoped lang="scss">

.articles-add ::v-deep .ql-editor{
  height: 200px;
}
.articles-add ::v-deep .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
